package barcode.code.nhat.iuhbarcode;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import Database.Dialogtoshow;
import Database.SQLquery;
import Library.ConnectionDetector;

public class Login_Action extends Activity {


    private Button b1, b2;
    private EditText e1,e2,o1,o2;
    private Boolean isInternetPresent = false;
    private ConnectionDetector cd;
    private Dialogtoshow dc = new Dialogtoshow();
    private LinearLayout l1,l2;
    private SQLquery login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        login = new SQLquery(this);
        login = login.open();
        l1 = (LinearLayout) findViewById(R.id.online);
        l2 = (LinearLayout) findViewById(R.id.offline);
        cd = new ConnectionDetector(getApplicationContext());
        e1 = (EditText) findViewById(R.id.ed_user);
        e2 = (EditText) findViewById(R.id.ed_pass);
        o1 = (EditText) findViewById(R.id.ed_teacher_id);
        o2 = (EditText) findViewById(R.id.ed_class_id);

        // get Internet status
        isInternetPresent = cd.isConnectingToInternet();

        // check for Internet status
        if (isInternetPresent) {
            dc.showAlertDialog(Login_Action.this, "Internet Connection",
                    "Android is online", true);

            l1.setVisibility(View.VISIBLE);
            l2.setVisibility(View.INVISIBLE);
            b1 = (Button) findViewById(R.id.bt_sign_in);
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String a = String.valueOf(e1.getText());
                    String b = String.valueOf(e2.getText());
                    if(a.equals("")){
                        e1.setError("Please fill in form!");
                    } else
                    if(b.equals("")){
                        e2.setError("Please fill in form!");
                    }else {
                        Intent i = new Intent(getApplication(), Main_Action.class);
                        startActivity(i);
                    }

                }
            });
        } else {
            dc.showAlertDialog(Login_Action.this, "No Internet Connection",
                    "Android work offline", false);
            l2.setVisibility(View.VISIBLE);
            l1.setVisibility(View.INVISIBLE);
            b1 = (Button) findViewById(R.id.bt_come_in);
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(getApplication(),Main_Action.class);
                    startActivity(i);
                }
            });
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        login.close();
    }
}
